﻿//mini.parse();

//要删除对象，表名和ID;对象模式{table:"tablename",id:"id值"};
var _removeData = [];
function fnFormRemove(table,key_field,key_value) {
    var entity = {};
    entity.table = table;
    entity.key_field = key_field;
    entity.key_value = key_value;
    _removeData.push(entity);
}
//add by zengtongchun 2016-12-28 刷新缓存
function fnRefreshCache(key) {
    $.ajax({
        url: "/Share/RefreshCache",
        type: "post",
        data: { cacheName: key },
        cache: false,
        success: function (text) {
           
        },
        error: function (jqXHR, textStatus, errorThrown) {
           
        }
    });
}
function fnRefreshCacheList(key) {
    var cacheList = fnGetData("v_data_base_dictionary_detail", "code='sys_cache'").data;
    for (var i = 0; i < cacheList.length; i++) {
        if (key == cacheList[i].value) {
            fnRefreshCache(cacheList[i].name);
        }
    }
}

function fnSave() {
    ///数据检查
    if (typeof (fnCustomDataCheck) != "undefined") {
        var msg = fnCustomDataCheck();
        if (msg != "") {
            alert(msg);
            return;
        }
    }
    ///客制化数据保存
    if (typeof (fnCustomSave) != "undefined") {
        fnCustomSave();
        return;
    }

    var form = new mini.Form("#form1");
    form.validate();
    if (!form.isValid()) return;
    var form_data = form.getData();
    fnClone(form_data, instance);
    if (typeof (instance) != "undefined" && instance != null) {
        var msg = mini.loading("数据保存中，请稍后......", "系统消息提醒");
        var json = mini.encode(instance);   //序列化成JSON
        $.ajax({
            url: "/extendpage/action/datasave.aspx?entity=" + const_EditEntity + "&flag=1&action=" + action,
            type: "post",
            data: { data: json },
            cache: false,
            success: function (text) {
                if (text == 1) {
                    mini.alert("数据保存成功!");
                    fnRefreshCacheList(const_EditEntity);
                    fnClose();
                    

                } else {
                    mini.alert(text);
                    //alert("数据保存失败!")
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert(jqXHR.responseText);
            }
        });
        mini.hideMessageBox(msg);
    }
}

///克隆对象
var fnClone2 = function (sourceObject, targetObject) {
    $.each(sourceObject, function (property, value) {
        if (property != "") {
            var _value = value;
            if (_value != null && _value != "") {
                if (_value.toString().indexOf("GMT") > 0) {
                    _value = mini.formatDate(_value, "yyyy-MM-dd H:mm:ss");
                }
            }
            targetObject[property] = $.trim(_value);

            //eval("targetObject." + property + "='" + value + "'");
        }
    });
};


///克隆对象
var fnClone = function (sourceObject, targetObject) {
    $.each(sourceObject, function (property, value) {
        if (property != "") {
            var _value = value;
            if (_value != null && _value != "") {
                if (_value.toString().indexOf("GMT") > 0) {
                    _value = mini.formatDate(_value, "yyyy-MM-dd H:mm:ss");
                }
                if (_value.toString().indexOf("1900-01-01") > -1) {
                    _value = null;
                }
            }
            if (_value == null) {
                targetObject[property] = null;
            } else {
                targetObject[property] = $.trim(_value);
            }
        }
    });
};
     



//反序列化实体对象到表单
function fnSetForm(obj) {
    var form = new mini.Form("#form1");
    if (!obj) {
        fnClone(instance, instance);
        form.setData(instance, false);
    }
    else
    {
        form.setData(obj, false);
    }
}





//反序列化实体对象到表单
function fnCustomeSetForm(formId, obj) {
    var form = new mini.Form("#" + formId);
    if (!fnClone) {
        fnClone = function (sourceObject, targetObject) {
            $.each(sourceObject, function (property, value) {
                if (property != "") {
                    var _value = value;
                    if (_value != null && _value != "") {
                        if (_value.toString().indexOf("GMT") > 0) {
                            _value = mini.formatDate(_value, "yyyy-MM-dd H:mm:ss");
                        }
                        if(_value.indexOf("1900")>-1)
                        {
                            _value = null;
                        }
                    }
                    if (_value == null) {
                        targetObject[property] =null;
                    } else {
                        targetObject[property] = $.trim(_value);
                    }

                    //eval("targetObject." + property + "='" + value + "'");
                }
            });
        };
    }

    fnClone(obj, obj);
    form.setData(obj, false);
}
function fnCustomGetData(formId) {
    var form = new mini.Form("#" + formId);
    return form.getData();
}


function fnCustomGetData(formId) {
    var form = new mini.Form("#" + formId);
    return form.getData();
}



function fnDataGridDataBing(grid, url) {
    var _grid = mini.get(grid);
    _grid.setUrl(url);
    _grid.reload();
}


var fnClose = function () {
    window.CloseOwnerWindow();
};


$(document).ready(function () {
    $("#btnSave").click(fnSave);
    $("#btnCancel").click(fnClose);
   
    var view_entity = getPar("view_entity");//显示的对应视图
    var view_data_entity = null;

    if (typeof (instance) != "undefined") {
        if (getPar("view_entity") == "") {
            fnSetForm();
        }else
        {
            view_data_entity = fnGetSingleData(view_entity, "id='" + instance.id + "'");
            fnSetForm(view_data_entity[0]);
        }
    }
    else {
        if(getPar("view_entity")!="")
        {
            view_data_entity = fnGetSingleData(view_entity, "id='" + instance.id + "'");
            fnSetForm(view_data_entity[0])
        }
    }
    //fnSetForm();
    fnKeyUp();
});


var fnKeyUp = function () {
    var length = $("input[type!=hidden]").length;
    $("input[type!=hidden]").keyup(function (e) {
        var key = e.which;
        if (13 == key) {
            var index = $("input[type!=hidden]").index(this);
            var newIndex = index + 1;
            if (length == newIndex) {
                newIndex = 0;
            }
            $("input[type!=hidden]:eq(" + newIndex + ")").focus();
        }
    });
}



//获取URL querystring parameter
function getPar1(source, name) {
    var reg = new RegExp("(^|\\?|&)" + name + "=([^&]*)(\\s|&|$)", "i");
    if (reg.test(source)) return RegExp.$2; return "";
}

function getPar(name) {
    var _url = "http://" + document.location;
    return getPar1(_url, name);
}

var fnUploadFileCallBack = function (data) {
    if (typeof (fnCustomUploadCallBack) == "function") {
        fnCustomUploadCallBack(data);
        return;
    }
    mini.alert(data);
}

//上传文件
var fnUploadFile = function () {
    if (typeof (fnCustomUploadFile) != "undefined") {
        fnCustomUploadFile();
        return;
    }
    //var row = fnGetSelectedRow();
    mini.open({
        url: "/extendpage/upload/uploadfile.aspx?entity=" + const_EditEntity + "&functionid=" + const_functionId + "&action=importdata&id=",
        title: "上传文件", width: 450, height: 300,
        onload: function () {
            var iframe = this.getIFrameEl();
            //var data = { action: "new" };
            //var data = iframe.contentWindow.SetData(data);
        },
        ondestroy: function (action) {
            if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.fnGetData();
                fnUploadFileCallBack(data);
            }
        }
    });
}



//上传多个文件
var fnUploadMultiFile = function () {
    if (typeof (fnCustomUploadFile) != "undefined") {
        fnCustomUploadFile();
        return;
    }
    //var row = fnGetSelectedRow();
    mini.open({
        url: "/extendpage/upload/uploadfile.aspx?entity=" + const_EditEntity + "&functionid=" + const_functionId + "&action=importdata&id=&limit=10",
        title: "上传文件", width: 450, height: 300,
        onload: function () {
            var iframe = this.getIFrameEl();
            //var data = { action: "new" };
            //var data = iframe.contentWindow.SetData(data);
        },
        ondestroy: function (action) {
            if (action == "ok") {
                var iframe = this.getIFrameEl();
                var data = iframe.contentWindow.fnGetData();
                fnUploadFileCallBack(data);
            }
        }
    });
}

function fnGetToday() {
    var today = new Date();
    var day = today.getDate();
    var month = today.getMonth() + 1;
    var year = today.getFullYear();
    var date = year + "-" + month + "-" + day;
    return date;
}


function newguid() {
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
        var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
        return v.toString(16);
    });
}

